package cl.whyem.spring.integration.mail.config;

import cl.whyem.spring.integration.mail.spring.CustomSearchTermStrategy;
import cl.whyem.spring.integration.mail.spring.OnlyOnceTrigger;
import java.io.File;
import java.util.concurrent.CountDownLatch;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.core.io.AbstractResource;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

@Configuration
@ComponentScan(basePackages = "cl.whyem.spring")
@ImportResource({"classpath*:integration-context.xml"})
public class RootConfig {

    @Bean
    public static PropertyPlaceholderConfigurer properties() {
        Resource[] resources = new AbstractResource[]{
            new ClassPathResource("mail-properties.properties"),
            new FileSystemResource(new File(System.getProperty("user.home") + File.separator + ".ahorro" + File.separator + "ahorro.properties"))
        };
        PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
        ppc.setIgnoreUnresolvablePlaceholders(true);
        ppc.setIgnoreResourceNotFound(true);
        ppc.setSystemPropertiesMode(2);
        ppc.setLocations(resources);
        return ppc;
    }
//
//    @Value("${jdbc.driver.class}")
//    private String driverClassName;
//    @Value("${jdbc.url}")
//    private String jdbcUrl;
//    @Value("${jdbc.user}")
//    private String jdbcUser;
//    @Value("${jdbc.password}")
//    private String jdbcPassword;
//
//    @Bean(name = "PersistenciaDS")
//    public DataSource PersistenciaDS() {
//        DriverManagerDataSource dataSource = new DriverManagerDataSource();
//        dataSource.setDriverClassName(driverClassName);
//        dataSource.setUrl(jdbcUrl);
//        dataSource.setUsername(jdbcUser);
//        dataSource.setPassword(jdbcPassword);
//        return dataSource;
//    }

    @Bean(name = "onlyOnceTrigger")
    public OnlyOnceTrigger onlyOnceTrigger() {
        return new OnlyOnceTrigger();
    }

    @Bean(name = "searchTermStrategy")
    public CustomSearchTermStrategy customSearchTermStrategy() {
        return new CustomSearchTermStrategy();
    }

    @Bean(name = "countDownLatch")
    public CountDownLatch countDownLatch() {
        return new CountDownLatch(1);
    }

}
